﻿/* 2014/8/4 */
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml.Serialization;

namespace PictureManager.Option
{
    class DynamicColumnExtractionRuleSerializer
    {
        private static XmlSerializer _serializer;

        static DynamicColumnExtractionRuleSerializer()
        {
            _serializer = new XmlSerializer(typeof(DynamicColumnExtractionRule));
        }

        public static string Serialize(DynamicColumnExtractionRule rule)
        {
            string xml = null;
            TextWriter textWriter = null;
            try
            {
                textWriter = new StringWriter();
                _serializer.Serialize(textWriter, rule);

                textWriter.Flush();
                xml = textWriter.ToString();
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
                Debug.WriteLine(ex.StackTrace);
            }
            finally
            {
                if (textWriter != null)
                    textWriter.Close();
            }

            return xml;
        }

        public static DynamicColumnExtractionRule Deserialize(string xml)
        {
            DynamicColumnExtractionRule rule = null;
            TextReader textReader = null;
            try
            {
                textReader = new StringReader(xml);
                rule = (DynamicColumnExtractionRule)_serializer.Deserialize(textReader);
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
                Debug.WriteLine(ex.StackTrace);
            }
            finally
            {
                if (textReader != null)
                    textReader.Close();
            }

            return rule;
        }
    }
}
